package com.cch.demo.sys.service.impl;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.creator.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cch.demo.sys.domain.AdataSource;
import com.cch.demo.sys.dto.DataSourceDTO;
import com.cch.demo.sys.mapper.AdataSourceMapper;
import com.cch.demo.sys.service.AdataSourceService;

import lombok.AllArgsConstructor;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.sql.DataSource;


@Service
@DS("master")
@AllArgsConstructor
public class AdataSourceServiceImpl extends ServiceImpl<AdataSourceMapper, AdataSource> implements AdataSourceService {

	private final DataSource dataSource;
    private final HikariDataSourceCreator hikariDataSourceCreator;
    
	@Override
    @DS("master")//这里必须包一层，不能调用mp默认的插入，因为会走到从库去
    public List<AdataSource> selectList(Wrapper<AdataSource> queryWrapper) {
        List<AdataSource> selectList = baseMapper.selectList(queryWrapper);
		return selectList;
    }

	
	/**
	 * 添加数据库所有数据源到
	 */
	public boolean addAllDataSourcesOnline() {
		DataSourceProperty dataSourceProperty = new DataSourceProperty();
        List<AdataSource> selectList = this.selectList(null);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        for (int i = 0; i < selectList.size(); i++) {
        	AdataSource dataSource2 = selectList.get(i);
        	DataSourceDTO dto = new DataSourceDTO();
	          dto.setPollName(dataSource2.getName());
	          dto.setUrl(dataSource2.getUrl());
	          dto.setUsername(dataSource2.getUserName());
	          dto.setPassword(dataSource2.getPassWord());
	          dto.setDriverClassName(dataSource2.getDatabasetype());
	        BeanUtils.copyProperties(dto, dataSourceProperty);
	        DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
	        ds.addDataSource(dto.getPollName(), dataSource);
		}
		return false;
	}


	@Override
	public boolean addDataSourceOnline(AdataSource adataSource) {
		try {
			DataSourceProperty dataSourceProperty = new DataSourceProperty();
	        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
	    	DataSourceDTO dto = new DataSourceDTO();
	          dto.setPollName(adataSource.getName());
	          dto.setUrl(adataSource.getUrl());
	          dto.setUsername(adataSource.getUserName());
	          dto.setPassword(adataSource.getPassWord());
	          dto.setDriverClassName(adataSource.getDatabasetype());
	        BeanUtils.copyProperties(dto, dataSourceProperty);
	        DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
	        ds.addDataSource(dto.getPollName(), dataSource);
	        return true;
		} catch (Exception e) {
			return false;
		}
	}

}
